package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.enums.SmsTypeEnums;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.TDeviceData;
import com.markerhub.service.ITDeviceDataService;
import com.markerhub.vo.DeviceVO;
import com.markerhub.vo.response.PersonInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 上报数据表前端控制器
 *
 * @author zhz
 * @since 2021-9-18 11:38:20
 */

@Slf4j
@Api(tags = "上报数据表", description = "上报数据表相关API")
@RestController
@RequestMapping("/device/data")
public class TDeviceDataController extends BaseController {

    @Autowired
    public ITDeviceDataService tDeviceDataService;

    /**
     * 新增
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody TDeviceData tDeviceData) {
        tDeviceDataService.save(tDeviceData);
        return Result.succ("");
    }

    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        tDeviceDataService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }

    /**
     * 条件查询
     */
    @ApiOperation(value = "条件查询", notes = "条件查询")
    @PostMapping("/get")
    public Result list(@RequestBody TDeviceData tDeviceData) {
        List<TDeviceData> tDeviceDataList = tDeviceDataService.list(new QueryWrapper<>(tDeviceData));
        return Result.succ(tDeviceDataList);
    }

    /**
     * 列表(分页)
     */
    @ApiOperation(value = "列表(分页)", notes = "列表(分页)")
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('device:data:list')")
    public Result pageList(@RequestBody DeviceVO deviceVO) {
        Page<TDeviceData> page = new Page<>(deviceVO.getCurrent(), deviceVO.getSize());
        Page<TDeviceData> dataPage = tDeviceDataService.page(page, new QueryWrapper<TDeviceData>().lambda().
                like(StringUtils.isNotBlank(deviceVO.getImsi()), TDeviceData::getImsi, deviceVO.getImsi()).
                eq(StringUtils.isNotBlank(deviceVO.getSmsType()), TDeviceData::getSmsType, deviceVO.getSmsType()).
                eq(null != deviceVO.getDeviceUpTime(), TDeviceData::getDeviceUpTime, deviceVO.getDeviceUpTime()).
                orderByDesc(TDeviceData::getDeviceUpTime));
        dataPage.getRecords().forEach(item -> {
            item.setSmsType(SmsTypeEnums.getNameByCode(item.getSmsType()));
        });
        return Result.succ(dataPage);
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @GetMapping("/info/{id}")
    public Result get(@PathVariable("id") String id) {
        TDeviceData tDeviceData = tDeviceDataService.getById(id);
        return Result.succ(tDeviceData);
    }

    /**
     * 根据id修改
     */
    @ApiOperation(value = "根据id修改", notes = "根据id修改")
    @PostMapping("/update")
    public Result update(@RequestBody TDeviceData tDeviceData) {
        tDeviceDataService.updateById(tDeviceData);
        return Result.succ("");
    }

    @PostMapping("/signHandle/{id}")
    public Result signHandle(@PathVariable("id") Long id) {
        tDeviceDataService.signHandle(id);
        return Result.succ("");
    }

    @ApiOperation(value = "查询健康码数据", notes = "查询健康码信息列表")
    @ApiImplicitParam(
            name = "robotCode",dataType = "String",
            dataTypeClass = String.class,value = "机器人编号",
            required = true,paramType = "query"
    )
    @GetMapping("/getDate")
    public Result<List<PersonInfoVO>> getDate(@ApiParam(value = "请求凭证")
                                                  @RequestHeader(name = "Authorization")String authorization,
                                                  @ApiParam(value = "机器人编号")
                                                  @RequestParam(name = "robotCode") String robotCode){
        List<PersonInfoVO> dataList = tDeviceDataService.ShowDate();
        return Result.succ(dataList);
    }
}
